package com.example.rdcproject.ving.notes.util;

import com.example.rdcproject.ving.notes.entity.User;
import com.example.rdcproject.ving.notes.entity.Word;
import com.example.rdcproject.ving.notes.entity.WordBook;
import com.example.rdcproject.ving.notes.service.ConnectionPool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class LearnWordUtil extends JFrame{
    User user = User.getInstance();
    WordBook wordBook =  WordBook.getInstance();


    private List<Word> wordList = wordBook.getWords();


    private static  LearnWordUtil instance = null;

    public List<Word> getWordList() {
        return wordList;
    }

    private LearnWordUtil() {}

    public static LearnWordUtil getInstance(){
        if(instance == null){
            instance = new LearnWordUtil();
        }
           return  instance;
    }

    //添加词书
    public void createWordList()  {

        JFrame frame = new JFrame("添加词书");
        frame.setLayout(new FlowLayout());
        frame.setSize(500,500);

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        frame.add(panel);

        JLabel label = new JLabel("请输入你想添加的词书名字");
         label.setBounds(100,100,200,50);
         panel.add(label);

         JTextField textField = new JTextField();
         textField.setBounds(100,200,200,50);
         panel.add(textField);


         JButton button = new JButton("确定添加");
         button.setBounds(300,350,70,50);
         panel.add(button);
         button.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 String name =textField.getText();
                 addBook(name);
             }
         });


        frame.pack();
        frame.setVisible(true);
       // frame.dispose();
    }

    public void  addBook(String bookName) {


        ConnectionPool connectionPool = null;
        Connection connection = null;
        Statement statement = null;
        try {
            connectionPool = ConnectionPool.getInstance();
            connection = connectionPool.getConnection();
            statement = connection.createStatement();

            connection.setAutoCommit(false);

            wordList = new ArrayList<>();

            //将词书添加到用户私有的词书表
            String sql = "drop table  if exists " + user.getNickname() + "WordBook ";
            statement.execute(sql);

            String sql2 = "create table " + user.getNickname() + "WordBook like " + bookName;
            statement.execute(sql2);

            String sql3 = "insert into " + user.getNickname() + "WordBook select * from " + bookName;

            String sql4 = "ALTER TABLE " + user.getNickname() + "WordBook AUTO_INCREMENT = 1 ";

            statement.addBatch(sql3);
            statement.addBatch(sql4);

            statement.executeBatch();

            String sql5 = "select * from " + user.getNickname() + "WordBook";
            statement = connection.prepareStatement(sql5);

            ResultSet resultSet = statement.executeQuery(sql5);

            //结果集解析
            while (resultSet.next()) {
                Word word = new Word();
                String wordEnglish = resultSet.getString("word");
                String meaning = resultSet.getString("meaning");
                word.setWord(wordEnglish);
                word.setMeaning(meaning);
                wordList.add(word);

            }
            connection.commit();

            statement.close();
            connectionPool.releaseConnection(connection);
            System.out.println("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("你输入的词书不存在！");
            try {
                connection.rollback();
            } catch (SQLException ex) {
                System.out.println("事务回滚失败！");
            }
        }

    }

}
